package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.FlexibleStaffCardOrderMapper;
import com.net.xpay.core.domain.FlexibleStaffCardOrder;
import com.net.xpay.core.helper.EnvironmentHelper;
import com.net.xpay.core.manager.FlexibleStaffCardOrderManager;
import com.net.common.manager.AbstractManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;


@Component
public class FlexibleStaffCardOrderManagerImpl extends AbstractManager<FlexibleStaffCardOrder> implements FlexibleStaffCardOrderManager{

    @Autowired
    private FlexibleStaffCardOrderMapper mapper;


    @Override
    protected BasicMapper<Long, FlexibleStaffCardOrder> getMapper() {
        return mapper;
    }

    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private EnvironmentHelper environmentHelper;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(FlexibleStaffCardOrder obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        //外部交易号
        obj.setOutBizNo(environmentHelper.getEnv() + "-" + obj.getUserId() + "-" + obj.getId());
        return super.save(obj);
    }

    @Override
    public FlexibleStaffCardOrder getByOutBizNo(String outBizNo) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(outBizNo));
        FlexibleStaffCardOrder condition = new FlexibleStaffCardOrder();
        condition.setOutBizNo(outBizNo);
        return getByCondition(condition);

    }

    @Override
    public FlexibleStaffCardOrder findByImportBizNo(Long userId, String importBizNo) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(importBizNo));
        FlexibleStaffCardOrder condition = new FlexibleStaffCardOrder();
        condition.setUserId(userId);
        condition.setImportBizNo(importBizNo);
        return getByCondition(condition);
    }
}
